package com.cy.sys.log.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.cy.sys.log.Dao.SysRoleUserDao;
import com.cy.sys.log.Dao.SysUserDao;
import com.cy.sys.log.pojo.pageObject;
import com.cy.sys.log.service.UserSerivce;
import com.cy.sys.log.service.exception.ServiceException;
import com.cy.sys.pojo.SysRoleMenus;
import com.cy.sys.pojo.SysUser;

@Service
public class SysUserSerivceimpl implements UserSerivce{
	@Autowired
	private SysUserDao sysuserdao;
	@Autowired
	private SysRoleUserDao sysroledao;
	
	//修改信息的思路及实现
	@Override
	public int updateObjectById(SysUser sysuser, Integer[] menuIds) {
		if(sysuser==null||menuIds==null||menuIds.length==0)
			throw new IllegalArgumentException("对不起你传递的参数不合法");
		//修改自身信息
		sysuserdao.updateObjectById(sysuser);
		//先根据id删除原有的关系网
		sysroledao.deleteMenuRoleId(sysuser.getId());
		//在新增关系网
		int ros = sysroledao.insertMenuRoleId(sysuser.getId(), menuIds);
		if(ros==0)
			throw new ServiceException("修改失败");
		return ros;
	}
	
	//多表查询
	@Override
	public SysRoleMenus findRoleById(Integer id) {
		if(id==0||id==null)
			throw new IllegalArgumentException("参数无效");
		SysRoleMenus sysrole = sysuserdao.findroleById(id);
		if(sysrole==null)
			throw new ServiceException("对不起没有找到");
		return sysrole;
	}
	
	
/**	方案单表查询
 * 	@Override
	public SysRoleMenus findRoleById(Integer id) {
		//校验id
		if(id==0||id<0)
			throw new IllegalArgumentException("参数不合法");
		SysRoleMenus sysrole = sysuserdao.findroleById(id);
		System.out.println(sysrole);
		if(sysrole==null)
			throw new ServiceException("没有找到内容");
		List<Integer> list = sysroledao.findRoleMenusById(id);
		System.out.println(list);
		
		sysrole.setMenuIds(list);
		return sysrole;
	}
*/
	@Override
	public int insertuserRoleObject(SysUser sysuser, Integer[] menuIds) {
		//参数校验
		if(sysuser.getName()==null)
			throw new IllegalArgumentException("参数值无效异常");
		if(menuIds==null||menuIds.length==0)
			throw new ServiceException("请选择对应的关联信息");
		int row = sysuserdao.insertuserRoleObject(sysuser);
		if(row==0)
			throw new ServiceException("创建失败");
		sysroledao.insertMenuRoleId(sysuser.getId(),menuIds);
		return row;
	}
	
	@Override
	public pageObject<SysUser> findAllUserObject(String name, Integer pageCurrent) {
		//校验传递的当前页码值是否有效
		if(pageCurrent==null||pageCurrent<1)
			throw new IllegalArgumentException("参数值无效");
		//查询总记录数
		int rowCount = sysuserdao.getUserRowCount(name);
		//校验查询到的有多少记录
		if(rowCount==0)
			throw new ServiceException("没有找到你查询的用户");
		//设置参数
		int pageSize = 2;
		//计算起始位置
		int pageCount = (pageCurrent-1)*pageSize;
		//查询信息
		List<SysUser> recosd = 
				sysuserdao.findAllUserObject(name, pageCount, pageSize);
		//创建对象并封装
		
		return new pageObject<>(pageSize, rowCount, pageCurrent, recosd);
	}

	
}
